package com.dz.lm.web.controller.newuser.attach;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.ImagesUtils;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.attach.Attach;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * 附件(视频) 控制层
 */
@Controller
@RequestMapping(value = "/h5/weijie/rewriteUrl/attach", produces = "application/json")
public class AttachController extends BasicControl {
    @Autowired
    private AttachService attachService;

    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");

        //参数校验
        Vector<String> inparam = new Vector<String>();
        //   inparam.add("mobilePhone");
        // inparam.add("password");
//		inparam.add("pushToken");
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //提交id
        long user_id = param.get("user_id") != null ? Long.parseLong(param.get("user_id").toString()) : 0;
        //腾讯云id
        String file_id = param.get("file_id") != null ? param.get("file_id").toString() : "";
        //附件所属的主数据的类型 1-用户视频
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 1;
        //1视频
        Integer attach_type = param.get("attach_type") != null ? Integer.parseInt(param.get("attach_type").toString()) : 1;
        //封面地址
        String cover_url = param.get("cover_url") != null ? param.get("cover_url").toString() : "";

        //附件名称
        String name = param.get("name") != null ? param.get("name").toString() : "";
        //附件地址
        String url = param.get("url") != null ? param.get("url").toString() : "";


        if (user_id == 0 || StringUtils.isEmpty(file_id)) {
            message = "参数有误";
        } else {
            Attach attach = new Attach();
            attach.setAttachType(attach_type);
            attach.setType(type);
            attach.setUrl(url);
            attach.setUserId(user_id);
            attach.setFileId(file_id);
            if(StringUtil.containEmpty(cover_url)){
                try {
                    Map<String, Object> screenshot = ImagesUtils.getScreenshot(url);
                    cover_url=screenshot.get("key") != null ? imgUrl + screenshot.get("key").toString() : "";
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
            attach.setCoverUrl(cover_url);
            attach.setName(name);
            Date day = new Date();
            attach.setCreateTime(DateUtil.dateToString(day));
            int add = 0;
            add = attachService.add(attach);
            if (add > 0) {
                message = "添加成功";
                result = "00";
                Map map = new HashMap();
                map.put("id",attach.getAttachId());
                data = map;
            }
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 删除附件
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //   inparam.add("mobilePhone");
        // inparam.add("password");
//		inparam.add("pushToken");
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        long attach_id = param.get("attach_id") != null ? Long.parseLong(param.get("attach_id").toString()) : 0;
        if (attach_id == 0) {
            message = "参数有误";
        } else {
            int ret = 0;
            ret = attachService.delete(attach_id);
            if (ret > 0) {
                message = "删除成功";
                result = "00";
            }
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 获取用户视频
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";

        Map<String, Object> param = JsonUtils.readValue(reqData);

        //参数校验
        Vector<String> inparam = new Vector<String>();
        // inparam.add("mobilePhone");
        //inparam.add("password");
        //inparam.add("pushToken");
        //1用户 2产品
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 1;
        long user_id = param.get("user_id") != null ? Long.parseLong(param.get("user_id").toString()) : 0;
        Integer page = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        Integer attachType = param.get("attachType") != null ? Integer.parseInt(param.get("attachType").toString()) : 1;

        if (user_id == 0) {
            message = "参数有误";
        } else {
            data = attachService.query(page,rows,type,attachType,0,"",user_id);
            message = "获取成功";
            result = "00";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }

    }
}
